package com.woniuxy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.mapper.FreightMapper;
import com.woniuxy.mapper.FreightTemplateMapper;
import com.woniuxy.mapper.ShippingCompanyMapper;
import com.woniuxy.mapper.ShippingTemplateMapper;
import com.woniuxy.pojo.Freight;
import com.woniuxy.pojo.FreightTemplate;
import com.woniuxy.pojo.ShippingCompany;
import com.woniuxy.pojo.ShippingTemplate;
import com.woniuxy.pojo.dto.FreightDTO;
import com.woniuxy.pojo.dto.ShippingTemplateDTO;
import com.woniuxy.pojo.dto.TemplateDTO;
import com.woniuxy.pojo.vo.ShippingTemplateVO;
import com.woniuxy.pojo.vo.ShippingVO;
import com.woniuxy.service.ShippingTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 29331
* @description 针对表【t_shipping_template(运费模板表)】的数据库操作Service实现
* @createDate 2025-06-06 10:45:41
*/
@Service
public class ShippingTemplateServiceImpl extends ServiceImpl<ShippingTemplateMapper, ShippingTemplate>
    implements ShippingTemplateService {

    @Autowired
    private ShippingTemplateMapper shippingTemplateMapper;
    @Autowired
    private FreightTemplateMapper freightTemplateMapper;

    @Autowired
    private ShippingCompanyMapper shippingCompanyMapper;

    @Autowired
    private FreightMapper freightMapper;

    @Override
    public List<ShippingVO> getAllData() {
        List<ShippingVO> vos = new ArrayList<>();
        List<FreightTemplate> freightTemplates = freightTemplateMapper.selectList(null);
        for (FreightTemplate freightTemplate : freightTemplates){

            ShippingVO shippingVO = new ShippingVO();
            shippingVO.setId(freightTemplate.getId());
            shippingVO.setTemplateName(freightTemplate.getTemplateName());
            shippingVO.setStatus(freightTemplate.getBillingRules());
            LambdaQueryWrapper<ShippingTemplate> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShippingTemplate::getFreightTemplateId,freightTemplate.getId());
            List<ShippingTemplate>list = shippingTemplateMapper.selectList(wrapper);
            List<ShippingTemplateVO> shippingTemplateVOS = new ArrayList<>();
            for (ShippingTemplate shippingTemplate : list){
                LambdaQueryWrapper<ShippingCompany>wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(ShippingCompany::getId,shippingTemplate.getExpressCompany());
                ShippingTemplateVO vo = new ShippingTemplateVO();
                BeanUtils.copyProperties(shippingTemplate,vo);
                vo.setExpressCompany(shippingCompanyMapper.selectOne(wrapper1).getName());
                shippingTemplateVOS.add(vo);
            }




            shippingVO.setTemplates(shippingTemplateVOS);
            vos.add(shippingVO);
        }



        return vos;
    }

    @Override
    public void addShippingTemplate(ShippingTemplateDTO shippingTemplateDTO) {
       FreightTemplate freightTemplate = new FreightTemplate();
       freightTemplate.setId(freightTemplate.getId());
       freightTemplate.setTemplateName(shippingTemplateDTO.getTemplateName());
       freightTemplate.setProvince(shippingTemplateDTO.getProvince());
       freightTemplate.setCity(shippingTemplateDTO.getCity());
       freightTemplate.setArea(shippingTemplateDTO.getArea());
       freightTemplate.setBear(shippingTemplateDTO.getBear());
       freightTemplate.setExpressCompany(shippingTemplateDTO.getExpressCompany());
       freightTemplate.setBillingRules(shippingTemplateDTO.getBillingRules());
       freightTemplate.setIsFreight(shippingTemplateDTO.getIsFreight());
        freightTemplateMapper.insert(freightTemplate);
        Long templateId = freightTemplate.getId();

        if (freightTemplate.getIsFreight() == 1){
            List<FreightDTO> freightDTOS = shippingTemplateDTO.getFreight();
            if (freightDTOS != null && !freightDTOS.isEmpty()) {
                for (FreightDTO freightDTO : freightDTOS) {
                    Freight freight = new Freight();
                    freight.setAddress(JSON.toJSONString(freightDTO.getAddress())); // 把 List<String> 转成逗号分隔字符串
                    freight.setShippingId(freightDTO.getShippingId());
                    freight.setConditions(Integer.parseInt(freightDTO.getCondition())); // condition 是 String 类型，转成 Integer
                    freight.setFreightTemplateId(templateId); // 关联模板 ID
                    freightMapper.insert(freight);
                }
            }
        }

        List<TemplateDTO> templateDTOS = shippingTemplateDTO.getDestinations();
        if (templateDTOS !=null && !templateDTOS.isEmpty()){
            for (TemplateDTO dto : templateDTOS){

                ShippingTemplate shippingTemplate = new ShippingTemplate();
                shippingTemplate.setExpressCompany(shippingTemplateDTO.getExpressCompany());
                shippingTemplate.setLow(dto.getLow());
                shippingTemplate.setMid(dto.getMid());
                shippingTemplate.setHigh(dto.getHigh());
                shippingTemplate.setAddWeight(dto.getAddWeight());
                shippingTemplate.setAmount(dto.getAmount());
                shippingTemplate.setAddAmount(dto.getAddAmount());
                shippingTemplate.setVolume(dto.getVolume());
                shippingTemplate.setAddVolume(dto.getAddVolume());
                shippingTemplate.setStatus(dto.getStatus());
                shippingTemplate.setFreightTemplateId(templateId);
                shippingTemplate.setDestination(JSON.toJSONString(dto.getDestination()));
                shippingTemplateMapper.insert(shippingTemplate);
            }
        }
    }

    @Override
    public void deleteShippingById(Long id) {
        LambdaQueryWrapper<ShippingTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShippingTemplate::getFreightTemplateId,id);
        shippingTemplateMapper.delete(wrapper);
        freightTemplateMapper.deleteById(id);
    }
}




